<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile
?>
<script>
require([
    'jquery',
    'Magento_Ui/js/modal/confirm',
    'underscore',
    'mage/adminhtml/grid',
    'prototype'
], function(jQuery, confirm, _){
<!--
<?php $myBlock = $block->getLayout()->getBlock('roleUsersGrid'); ?>
<?php if (is_object($myBlock) && $myBlock->getJsObjectName()): ?>
    var checkBoxes = $H(<?php /* @escapeNotVerified */ echo $myBlock->getUsers(true) ?>);
    var warning = false;
    if (checkBoxes.size() > 0) {
        warning = true;
    }
    jQuery('#in_role_user').val(checkBoxes.toQueryString());

    function registerUserRole(grid, element, checked)
    {
        if (checked) {
            checkBoxes.set(element.value, 0);
        } else {
            checkBoxes.unset(element.value);
        }
        $('in_role_user').value = checkBoxes.toQueryString();
        grid.reloadParams = {'in_role_user[]':checkBoxes.keys()};
    }

    function roleUsersRowClick(grid, event)
    {
        var trElement = Event.findElement(event, 'tr');
        var isInput   = Event.element(event).tagName == 'INPUT';
        if (trElement) {
            var checkbox = Element.getElementsBySelector(trElement, 'input');
            if(checkbox[0]){
                var checked = isInput ? !checkbox[0].checked : checkbox[0].checked;

                if (checked) {
                    confirm({
                        content: "<?php /* @escapeNotVerified */ echo __('Warning!\r\nThis action will remove this user from already assigned role\r\nAre you sure?') ?>",
                        actions: {
                            confirm: function () {
                                checkbox[0].checked = false;
                                checkBoxes.unset(checkbox[0].value);
                                varienElementMethods.setHasChanges(checkbox[0]);
                            },
                            cancel: function () {
                                checkbox[0].checked = true;
                            },
                            always: function () {
                                $('in_role_user').value = checkBoxes.toQueryString();
                                grid.reloadParams = {
                                    'in_role_user[]': checkBoxes.keys()
                                };
                            }
                        }
                    });
                } else {
                    checkbox[0].checked = true;
                    checkBoxes.set(checkbox[0].value, true);
                    $('in_role_user').value = checkBoxes.toQueryString();
                    grid.reloadParams = {
                        'in_role_user[]': checkBoxes.keys()
                    };
                }
            }
        }
    }

    function roleUsersRowInit(grid, row)
    {
        var checkbox = $(row).getElementsByClassName('checkbox')[0];
        if (checkbox) {
            if (checkbox.checked) {
                checkBoxes.set(checkbox.value, checkbox.checked);
            } else {
                checkBoxes.unset(checkbox.value);
            }

            $('in_role_user').value = checkBoxes.toQueryString();
            grid.reloadParams = {'in_role_user[]':checkBoxes.keys()};
        }
    }

    function massSelectUsers(allCheckbox) {
        if (!allCheckbox.checked && _.size(checkBoxes._object) > 0) {
            allCheckbox.checked = true;
            confirm({
                content: "<?php /* @escapeNotVerified */ echo __('Warning!\r\nThis action will remove those users from already assigned roles\r\nAre you sure?') ?>",
                actions: {
                    confirm: function () {
                        allCheckbox.checked = false;
                        markCheckboxes(false);
                    }
                }
            });
        } else {
            markCheckboxes(allCheckbox.checked)
        }
    }
    function markCheckboxes(value) {
        <?php /* @escapeNotVerified */ echo $myBlock->getJsObjectName() ?>.rows.each(function(row)
        {
            $(row).getElementsByClassName('checkbox')[0].checked = value;
            roleUsersRowInit(<?php /* @escapeNotVerified */ echo $myBlock->getJsObjectName() ?>, row);
        });
    }
    function onLoad() {
        if (typeof <?php /* @escapeNotVerified */ echo $myBlock->getJsObjectName() ?> !== 'undefined') {
            <?php /* @escapeNotVerified */ echo $myBlock->getJsObjectName() ?>.
            rowClickCallback = roleUsersRowClick;
            <?php /* @escapeNotVerified */ echo $myBlock->getJsObjectName() ?>.
            initRowCallback = roleUsersRowInit;
            <?php /* @escapeNotVerified */ echo $myBlock->getJsObjectName() ?>.
            checkboxCheckCallback = registerUserRole;
            <?php /* @escapeNotVerified */ echo $myBlock->getJsObjectName() ?>.
            checkCheckboxes = massSelectUsers;
            <?php /* @escapeNotVerified */ echo $myBlock->getJsObjectName() ?>.
            rows.each(function (row) {
                roleUsersRowInit(<?php /* @escapeNotVerified */ echo $myBlock->getJsObjectName() ?>, row)
            });
            $('in_role_user_old').value = $('in_role_user').value;
        } else {
            setTimeout(onLoad, 300);
        }
    }
    onLoad();
<?php endif; ?>
//-->

});
</script>
